/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/

// ----------------------------------------------------------------------------
// author: Daniel Lerch <dlerch@gmail.com>, http://opendomo.com
// date: January 2008
// description: Agent event notifications
//
// CHANGES:
//
// ----------------------------------------------------------------------------


#include <stdio.h>
#include <signal.h>

#include <util.h>
#include <upnpdev.h>
#include <upnplog.h>

#define UPNPDEV_DEVICE_PATH 	"/var/www/upnp/web-agent-events"
#define UPNPDEV_UDN "uuid:Upnp-OpenDomo-agent-events-1_0-000E35322D2C"
#define UPNPDEV_SERVICE_ID "urn:upnp-org:serviceId:control1"
#define UPNPDEV_SERVICE_TYPE "urn:schemas-upnp-org:service:control:1"

// {{{ upnpdev_subscriptions()
int upnpdev_subscriptions(struct Upnp_Subscription_Request *request)
{
   if(!request)
   {
      upnplog_printf(UPNPLOG_ERROR, "upnpdev_subscriptions() null request");
      return UPNP_E_BAD_REQUEST;
   }

   if(strcmp(request->UDN, UPNPDEV_UDN)!=0)
      return UPNP_E_SUCCESS;

   if(strcmp(request->ServiceId, UPNPDEV_SERVICE_TYPE)!=0)
   {
      upnplog_printf(UPNPLOG_ERROR, "upnpdev_subscriptions() "
         "service id doesn't exist");
      return UPNP_E_BAD_REQUEST;
   }

   IXML_Document *propset = NULL;
   char status_var[64];
   
   strcpy(status_var, "up"); // TODO: consultar su estado actual
   UpnpAddToPropertySet(&propset, "agent_status", status_var);

   UpnpAcceptSubscriptionExt(hdl, UPNPDEV_UDN, UPNPDEV_SERVICE_ID, 
      propset, request->Sid); 

   ixmlDocument_free(propset);

   return UPNP_E_SUCCESS;
}
// }}}

// {{{ upnpdev_callback()
int upnpdev_callback(Upnp_EventType event_type, void *event, void *cookie)
{
   int ret=0;

   switch(event_type) 
   {
      case UPNP_EVENT_SUBSCRIPTION_REQUEST:
      {
         ret = upnpdev_subscriptions(
            (struct Upnp_Subscription_Request*)event_type);  
         break;
      }

      case UPNP_CONTROL_GET_VAR_REQUEST:
      case UPNP_CONTROL_ACTION_REQUEST:
      case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
      case UPNP_DISCOVERY_SEARCH_RESULT:
      case UPNP_DISCOVERY_SEARCH_TIMEOUT:
      case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
      case UPNP_CONTROL_ACTION_COMPLETE:
      case UPNP_CONTROL_GET_VAR_COMPLETE:
      case UPNP_EVENT_RECEIVED:
      case UPNP_EVENT_SUBSCRIBE_COMPLETE:
      case UPNP_EVENT_UNSUBSCRIBE_COMPLETE:
      case UPNP_EVENT_RENEWAL_COMPLETE:
      case UPNP_EVENT_AUTORENEWAL_FAILED:
      case UPNP_EVENT_SUBSCRIPTION_EXPIRED:

      break;
   }

   return ret;
}
// }}}

int main(int argc, char **argv)
{
   sigset_t sigs_to_catch;
   int sig;

   // Start UPnP Device
   upnpdev_init(NULL, 0, "device.xml", UPNPDEV_DEVICE_PATH, upnpdev_callback);


   // Waiting signal
   sigemptyset(&sigs_to_catch);
   sigaddset(&sigs_to_catch, SIGINT);
   sigwait(&sigs_to_catch, &sig);

   // Stop
   util_print( "Shutting down on signal %d...\n", sig );
   upnpdev_cleanup();

   return 0;
}



